﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

[assembly: TagPrefix("WebModel", "webModel")]
namespace ActionModel.WebModel
{

	/// <summary>
	/// 分页链接控件
	/// </summary>
	[DefaultProperty("TotalCount")]
	[ToolboxData("<{0}:PaginationDiv runat=server></{0}:PaginationDiv>")]
	public class PaginationDiv : WebControl
	{
		//[Bindable(true)]
		//[Category("Appearance")]
		//[DefaultValue("")]
		//[Localizable(true)]
		//public string Text
		//{
		//    get
		//    {
		//        String s = (String)ViewState["Text"];
		//        return ((s == null) ? String.Empty : s);
		//    }

		//    set
		//    {
		//        ViewState["Text"] = value;
		//    }
		//}

		private int pageIndex;
		private int pageSize;
		private int totalCount;
		private PaginationShowType m_ShowType;
		/// <summary>
		/// 当前页码
		/// </summary>
		public int PageIndex { set { pageIndex = value; } }
		/// <summary>
		/// 每页显示条目数，默认为10
		/// </summary>
		public int PageSize { set { pageSize = value; } }
		/// <summary>
		/// 条目总数
		/// </summary>
		public int TotalCount { set { totalCount = value; } }
		/// <summary>
		/// 页码呈现方式
		/// </summary>
		public PaginationShowType ShowType
		{
			get { return m_ShowType; }
			set { m_ShowType = value; }
		}

		private string queryString;
		protected override void RenderContents(HtmlTextWriter output)
		{
			if (HttpContext.Current.Request.QueryString.ToString() == "")
			{
				queryString = "";
			}
			else
			{
				queryString = HttpContext.Current.Request.QueryString.ToString();
				queryString = new Regex("&?PageIndex=\\d+&PageSize=\\d+").Replace(queryString, "");
				if (queryString != "") queryString += "&";
			}

			pageSize = pageSize <= 0 ? 10 : pageSize;
			int pageCount = (int)Math.Ceiling(totalCount / (double)pageSize);
			switch (ShowType)
			{
				case PaginationShowType.Default:
					RenderContentsDefault(output, pageCount);
					break;
				case PaginationShowType.Simple:
					RenderContentsSimple(output, pageCount);
					break;
				case PaginationShowType.Grace:
					RenderContentsGrace(output, pageCount);
					break;
			}
		}

		private void RenderContentsSimple(HtmlTextWriter output, int pageCount)
		{
			if (pageIndex == 1)
			{
				output.Write("<span class=\"FirstPage\">【最前页】</span>");
				output.Write("&nbsp;");
				output.Write("<span class=\"PrevPage\">【上一页】</span>");
			}
			else
			{
				output.Write("<a class=\"FirstPage\" href=\"?" + queryString + "PageIndex=1&PageSize=" + pageSize + "\">【最前页】</a>");
				output.Write("&nbsp;");
				output.Write("<a class=\"PrevPage\" href=\"?" + queryString + "PageIndex=" + (pageIndex - 1).ToString() + "&PageSize=" + pageSize + "\">【上一页】</a>");
			}
			if (pageIndex == pageCount)
			{
				output.Write("<span class=\"NextPage\">【下一页】</span>");
				output.Write("&nbsp;");
				output.Write("<span class=\"LastPage\">【最后页】</span>");
			}
			else
			{
				output.Write("&nbsp;");
				output.Write("<a class=\"NextPage\" href=\"?" + queryString + "PageIndex=" + (pageIndex + 1).ToString() + "&PageSize=" + pageSize + "\">下一页</a>");
				output.Write("&nbsp;");
				output.Write("<a class=\"LastPage\" href=\"?" + queryString + "PageIndex=" + pageCount.ToString() + "&PageSize=" + pageSize + "\">【最后页】</a>");
			}
			output.Write("&nbsp;");
			output.Write("<span class=\"CountPage\">第" + pageIndex.ToString() + "页</span>");
			output.Write("&nbsp;");
			output.Write("<span class=\"CountPage\">共" + pageCount.ToString() + "页</span>");
			output.Write("&nbsp;");
			output.Write("<span class=\"CountPage\">共" + totalCount.ToString() + "条记录</span>");

			output.Write("</div>");
		}

		private void RenderContentsDefault(HtmlTextWriter output, int pageCount)
		{
			output.Write("<div class=\"PaginationDiv\">");
			if (pageIndex == 1)
			{
				output.Write("<span class=\"PrevPage\">上一页</span>");
			}
			else
			{
				output.Write("<a class=\"PrevPage\" href=\"?" + queryString + "PageIndex=" + (pageIndex - 1).ToString() + "&PageSize=" + pageSize + "\">上一页</a>");
			}
			if (pageCount < 10)
			{
				for (int i = 1; i <= pageCount; i++)
				{
					if (pageIndex == i)
					{
						output.Write("<span class=\"current\">" + i + "</span>");
					}
					else
					{
						output.Write("<a href=\"?" + queryString + "PageIndex=" + i + "&PageSize=" + pageSize + "\">" + i + "</a>");
					}
				}
			}
			else
			{
				if (pageIndex <= 5)
				{
					for (int i = 1; i <= 7; i++)
					{
						if (pageIndex == i)
						{
							output.Write("<span class=\"current\">" + i + "</span>");
						}
						else
						{
							output.Write("<a href=\"?" + queryString + "PageIndex=" + i + "&PageSize=" + pageSize + "\">" + i + "</a>");
						}
					}
					output.Write("<span class=\"ellipsis\">…</span>");
					output.Write("<a href=\"?" + queryString + "PageIndex=" + pageCount + "&PageSize=" + pageSize + "\">" + pageCount + "</a>");
				}
				else if (pageIndex > pageCount - 5)
				{
					output.Write("<a href=\"?" + queryString + "PageIndex=1&PageSize=" + pageSize + "\">1</a>");
					output.Write("<span class=\"ellipsis\">…</span>");
					for (int i = pageCount - 7 + 1; i <= pageCount; i++)
					{
						if (pageIndex == i)
						{
							output.Write("<span class=\"current\">" + i + "</span>");
						}
						else
						{
							output.Write("<a href=\"?" + queryString + "PageIndex=" + i + "&PageSize=" + pageSize + "\">" + i + "</a>");
						}
					}
				}
				else
				{
					output.Write("<a href=\"?" + queryString + "PageIndex=1&PageSize=" + pageSize + "\">1</a>");
					output.Write("<span class=\"ellipsis\">…</span>");
					for (int i = pageIndex - 2; i <= pageIndex + 2; i++)
					{
						if (pageIndex == i)
						{
							output.Write("<span class=\"current\">" + i + "</span>");
						}
						else
						{
							output.Write("<a href=\"?" + queryString + "PageIndex=" + i + "&PageSize=" + pageSize + "\">" + i + "</a>");
						}
					}
					output.Write("<span class=\"ellipsis\">…</span>");
					output.Write("<a href=\"?" + queryString + "PageIndex=" + pageCount + "&PageSize=" + pageSize + "\">" + pageCount + "</a>");
				}
			}

			if (pageIndex == pageCount)
			{
				output.Write("<span class=\"NextPage\">下一页</span>");
			}
			else
			{
				output.Write("<a class=\"NextPage\" href=\"?" + queryString + "PageIndex=" + (pageIndex + 1).ToString() + "&PageSize=" + pageSize + "\">下一页</a>");
			}
			output.Write("</div>");

		}

		private void RenderContentsGrace(HtmlTextWriter output, int pageCount)
		{
			output.Write("<div class=\"PaginationDiv\">");
			if (pageIndex == 1)
			{
				output.Write("<span class=\"PrevPage\">上一页&nbsp;</span>");
			}
			else
			{
				output.Write("<a class=\"PrevPage\" href=\"?" + queryString + "PageIndex=" + (pageIndex - 1).ToString() + "&PageSize=" + pageSize + "\">上一页</a>");
			}
			if (pageCount < 5)
			{
				for (int i = 1; i <= pageCount; i++)
				{
					if (pageIndex == i)
					{
						output.Write("<span class=\"current\">" + i + "</span>");
					}
					else
					{
						output.Write("<a href=\"?" + queryString + "PageIndex=" + i + "&PageSize=" + pageSize + "\">" + i + "</a>");
					}
				}
			}
			else
			{
				if (pageIndex <= 5)
				{
					for (int i = 1; i <= 5; i++)
					{
						if (pageIndex == i)
						{
							output.Write("<span class=\"current\">" + i + "</span>");
						}
						else
						{
							output.Write("<a href=\"?" + queryString + "PageIndex=" + i + "&PageSize=" + pageSize + "\">" + i + "</a>");
						}
					}
					output.Write("<span class=\"ellipsis\">…</span>");
					output.Write("<a href=\"?" + queryString + "PageIndex=" + pageCount + "&PageSize=" + pageSize + "\">" + pageCount + "</a>");
				}
				else if (pageIndex > pageCount - 5)
				{
					output.Write("<a href=\"?" + queryString + "PageIndex=1&PageSize=" + pageSize + "\">1</a>");
					output.Write("<span class=\"ellipsis\">…</span>");
					for (int i = pageCount - 5 + 1; i <= pageCount; i++)
					{
						if (pageIndex == i)
						{
							output.Write("<span class=\"current\">" + i + "</span>");
						}
						else
						{
							output.Write("<a href=\"?" + queryString + "PageIndex=" + i + "&PageSize=" + pageSize + "\">" + i + "</a>");
						}
					}
				}
				else
				{
					output.Write("<a href=\"?" + queryString + "PageIndex=1&PageSize=" + pageSize + "\">1</a>");
					output.Write("<span class=\"ellipsis\">…</span>");
					for (int i = pageIndex - 2; i <= pageIndex + 2; i++)
					{
						if (pageIndex == i)
						{
							output.Write("<span class=\"current\">" + i + "</span>");
						}
						else
						{
							output.Write("<a href=\"?" + queryString + "PageIndex=" + i + "&PageSize=" + pageSize + "\">" + i + "</a>");
						}
					}
					output.Write("<span class=\"ellipsis\">…</span>");
					output.Write("<a href=\"?" + queryString + "PageIndex=" + pageCount + "&PageSize=" + pageSize + "\">" + pageCount + "</a>");
				}
			}

			if (pageIndex == pageCount)
			{
				output.Write("<span class=\"NextPage\">下一页</span>");
			}
			else
			{
				output.Write("<a class=\"NextPage\" href=\"?" + queryString + "PageIndex=" + (pageIndex + 1).ToString() + "&PageSize=" + pageSize + "\">下一页</a>");
			}
			output.Write("</div>");

		}
		protected override HtmlTextWriterTag TagKey
		{
			get
			{
				return HtmlTextWriterTag.Div;
			}
		}

		/// <summary>
		/// 页码呈现方式枚举
		/// </summary>
		public enum PaginationShowType
		{
			/// <summary>
			/// 无页码链接，罗列【最前页】【上一页】【下一页】【最后页】 第1页 共n页 共n条记录
			/// </summary>
			Simple,

			/// <summary>
			/// 默认9个页码链接，“…”略去中间页码
			/// </summary>
			Default,
			/// <summary>
			/// 默认9个页码链接，“…”略去中间页码
			/// </summary>
			Grace,
		}
	}
}
